package com.walter.learn.UseThread.ThreadPool;

import com.walter.learn.ReadAndWrite.Read;
import com.walter.learn.ReadAndWrite.Write;
import com.walter.learn.UseThread.TestUseThreadPool;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @ProjectName: learnOwner
 * @Package: com.walter.learn.UseThread.ThreadPool
 * @ClassName: ThreadByReadAndWrite
 * @Description: java类作用描述
 * @Author: 唐朝
 * @CreateDate: 2019/2/19 17:42
 * @UpdateUser: 更新者
 * @UpdateDate: 2019/2/19 17:42
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
@Slf4j
public class ThreadByReadAndWrite {
    static List<String> list = new ArrayList();

    public static void main(String[] args) {
        ExecutorService pool = Executors.newCachedThreadPool();
        // ThreadByReadAndWrite tC=new ThreadByReadAndWrite();

        ReadThread runner1 = new ReadThread();
        TranformThread runner2 = new TranformThread();
        Thread t1 = new Thread(runner1);
        Thread t2 = new Thread(runner2);
        // 将线程放入池中进行执行
        pool.execute(t1);
        pool.execute(t2);
        pool.shutdown();
    }


    static class ReadThread implements Runnable {
        @Override
        public void run() {
            try {
                list = Read.readByInputStreamReader("G:\\TestData\\threadRead.txt");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    static class TranformThread implements Runnable {
        @Override
        public void run() {
            try {
                Thread.sleep(1000);
                for (int i = 0; i < list.size(); i++) {
                    Write.writeByFileOutputStream(list.get(i).toLowerCase() + "\r\n", "G:\\TestData\\threadPool01.txt");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}
